package ltd.hxya.novel.book.aspect;

import ltd.hxya.novel.common.constant.redis.RedisConstant;
import ltd.hxya.novel.common.utils.RedisUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class BookAspect {
    @Autowired
    private RedisUtils redisUtils;

    @Around("@annotation(ltd.hxya.novel.common.anno.AddVisitCount)")
    public Object visitCountHandler(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        String bookId=null;

        Object proceed = proceedingJoinPoint.proceed(args);
        for (Object arg : args) {
            if (arg instanceof String){
                bookId= (String) arg;
                break;
            }
        }
        redisUtils.incr(RedisConstant.BOOK_VISIT_COUNT+bookId);
        return proceed;
    }
}
